package com.flydog.taskManager.dto;

import com.flydog.taskManager.entity.PhasedTask;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
public class PhasedTaskWithChildren extends PhasedTask {
    private List<PhasedTaskWithChildren> children;

    public PhasedTaskTreeStructure paresToTreeStructure(Long selectId) {
        PhasedTaskTreeStructure result = new PhasedTaskTreeStructure();
        result.setKey(this.getId().toString());
        result.setTarget(this.getTarget());
        result.setTitle(this.getName());
        result.setProcessStatus(this.getProcessStatus());
        result.setSelected(this.getId().equals(selectId));
        if(children.size() > 0) {
            result.setIsLeaf(false);
            List<PhasedTaskTreeStructure> childrenItem = new ArrayList<>();
            for (PhasedTaskWithChildren child : children) {
                childrenItem.add(child.paresToTreeStructure(selectId));
                result.setChildren(childrenItem);
            }
        } else {
            result.setIsLeaf(true);
        }
        return result;
    }
}
